import { Topics } from './topic';
let _Topics = Topics;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import TopicMock from './mallm/pub/topicMock';
 * TopicMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/topic/listKey').reply(config => {
      let params = config.params;
      let mockTopics = _Topics.filter(topic => {
      	if(params.id||params.id==''){ if( topic.id.indexOf(params.id)>=0) return true;}
      	if(params.title||params.title==''){ if( topic.title.indexOf(params.title)>=0) return true;}
      	if(params.content||params.content==''){ if( topic.content.indexOf(params.content)>=0) return true;}
      	if(params.avatar||params.avatar==''){ if( topic.avatar.indexOf(params.avatar)>=0) return true;}
      	if(params.itemPicUrl||params.itemPicUrl==''){ if( topic.itemPicUrl.indexOf(params.itemPicUrl)>=0) return true;}
      	if(params.subtitle||params.subtitle==''){ if( topic.subtitle.indexOf(params.subtitle)>=0) return true;}
      	if(params.topicCategoryId||params.topicCategoryId==''){ if( topic.topicCategoryId.indexOf(params.topicCategoryId)>=0) return true;}
      	if(params.priceInfo||params.priceInfo==''){ if( topic.priceInfo.indexOf(params.priceInfo)>=0) return true;}
      	if(params.readCount||params.readCount==''){ if( topic.readCount.indexOf(params.readCount)>=0) return true;}
      	if(params.scenePicUrl||params.scenePicUrl==''){ if( topic.scenePicUrl.indexOf(params.scenePicUrl)>=0) return true;}
      	if(params.topicTemplateId||params.topicTemplateId==''){ if( topic.topicTemplateId.indexOf(params.topicTemplateId)>=0) return true;}
      	if(params.topicTagId||params.topicTagId==''){ if( topic.topicTagId.indexOf(params.topicTagId)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( topic.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.isShow||params.isShow==''){ if( topic.isShow.indexOf(params.isShow)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( topic.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockTopics
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/topic/list').reply(config => {
      let params = config.params;
      let mockTopics = _Topics.filter(topic => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(topic.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.title||params.title==''){if(topic.title.indexOf(params.title)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.content||params.content==''){if(topic.content.indexOf(params.content)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.avatar||params.avatar==''){if(topic.avatar.indexOf(params.avatar)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.itemPicUrl||params.itemPicUrl==''){if(topic.itemPicUrl.indexOf(params.itemPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.subtitle||params.subtitle==''){if(topic.subtitle.indexOf(params.subtitle)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.topicCategoryId||params.topicCategoryId==''){if(topic.topicCategoryId.indexOf(params.topicCategoryId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.priceInfo||params.priceInfo==''){if(topic.priceInfo.indexOf(params.priceInfo)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.readCount||params.readCount==''){if(topic.readCount.indexOf(params.readCount)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.scenePicUrl||params.scenePicUrl==''){if(topic.scenePicUrl.indexOf(params.scenePicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.topicTemplateId||params.topicTemplateId==''){if(topic.topicTemplateId.indexOf(params.topicTemplateId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.topicTagId||params.topicTagId==''){if(topic.topicTagId.indexOf(params.topicTagId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(topic.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isShow||params.isShow==''){if(topic.isShow.indexOf(params.isShow)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(topic.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockTopics
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockTopics.length;
      mockTopics = mockTopics.filter((topic, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockTopics,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/topic/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Topics = _Topics.filter(topic => topic.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/topic/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Topics = _Topics.filter(topic => !params.includes(topic.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/topic/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,title,content,avatar,itemPicUrl,subtitle,topicCategoryId,priceInfo,readCount,scenePicUrl,topicTemplateId,topicTagId,sortOrder,isShow,deptid } = params;
      _Topics.some(topic => {
        if (topic.id === id) {
			topic.id = id;
			topic.title = title;
			topic.content = content;
			topic.avatar = avatar;
			topic.itemPicUrl = itemPicUrl;
			topic.subtitle = subtitle;
			topic.topicCategoryId = topicCategoryId;
			topic.priceInfo = priceInfo;
			topic.readCount = readCount;
			topic.scenePicUrl = scenePicUrl;
			topic.topicTemplateId = topicTemplateId;
			topic.topicTagId = topicTagId;
			topic.sortOrder = sortOrder;
			topic.isShow = isShow;
			topic.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/topic/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,title,content,avatar,itemPicUrl,subtitle,topicCategoryId,priceInfo,readCount,scenePicUrl,topicTemplateId,topicTagId,sortOrder,isShow,deptid } = params;
      _Topics.push({
		id:id,
		title:title,
		content:content,
		avatar:avatar,
		itemPicUrl:itemPicUrl,
		subtitle:subtitle,
		topicCategoryId:topicCategoryId,
		priceInfo:priceInfo,
		readCount:readCount,
		scenePicUrl:scenePicUrl,
		topicTemplateId:topicTemplateId,
		topicTagId:topicTagId,
		sortOrder:sortOrder,
		isShow:isShow,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};